package DAOs;

import DAOs.exceptions.NonexistentEntityException;
import Entity.Vivarium;
import Entity.VivariumOperator;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
 *
 * @author USUARIO
 */
public class VivariumOperatorJpaController implements Serializable {

    public VivariumOperatorJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(VivariumOperator vivariumOperator) {
        if (vivariumOperator.getUlataVivariums() == null) {
            vivariumOperator.setUlataVivariums(new ArrayList<Vivarium>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<Vivarium> attachedUlataVivariums = new ArrayList<Vivarium>();
            for (Vivarium ulataVivariumsVivariumToAttach : vivariumOperator.getUlataVivariums()) {
                ulataVivariumsVivariumToAttach = em.getReference(ulataVivariumsVivariumToAttach.getClass(), ulataVivariumsVivariumToAttach.getId());
                attachedUlataVivariums.add(ulataVivariumsVivariumToAttach);
            }
            vivariumOperator.setUlataVivariums(attachedUlataVivariums);
            em.persist(vivariumOperator);
            for (Vivarium ulataVivariumsVivarium : vivariumOperator.getUlataVivariums()) {
                VivariumOperator oldVivariumOperatorOfUlataVivariumsVivarium = ulataVivariumsVivarium.getVivariumOperator();
                ulataVivariumsVivarium.setVivariumOperator(vivariumOperator);
                ulataVivariumsVivarium = em.merge(ulataVivariumsVivarium);
                if (oldVivariumOperatorOfUlataVivariumsVivarium != null) {
                    oldVivariumOperatorOfUlataVivariumsVivarium.getUlataVivariums().remove(ulataVivariumsVivarium);
                    oldVivariumOperatorOfUlataVivariumsVivarium = em.merge(oldVivariumOperatorOfUlataVivariumsVivarium);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(VivariumOperator vivariumOperator) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            VivariumOperator persistentVivariumOperator = em.find(VivariumOperator.class, vivariumOperator.getId());
            List<Vivarium> ulataVivariumsOld = persistentVivariumOperator.getUlataVivariums();
            List<Vivarium> ulataVivariumsNew = vivariumOperator.getUlataVivariums();
            List<Vivarium> attachedUlataVivariumsNew = new ArrayList<Vivarium>();
            for (Vivarium ulataVivariumsNewVivariumToAttach : ulataVivariumsNew) {
                ulataVivariumsNewVivariumToAttach = em.getReference(ulataVivariumsNewVivariumToAttach.getClass(), ulataVivariumsNewVivariumToAttach.getId());
                attachedUlataVivariumsNew.add(ulataVivariumsNewVivariumToAttach);
            }
            ulataVivariumsNew = attachedUlataVivariumsNew;
            vivariumOperator.setUlataVivariums(ulataVivariumsNew);
            vivariumOperator = em.merge(vivariumOperator);
            for (Vivarium ulataVivariumsOldVivarium : ulataVivariumsOld) {
                if (!ulataVivariumsNew.contains(ulataVivariumsOldVivarium)) {
                    ulataVivariumsOldVivarium.setVivariumOperator(null);
                    ulataVivariumsOldVivarium = em.merge(ulataVivariumsOldVivarium);
                }
            }
            for (Vivarium ulataVivariumsNewVivarium : ulataVivariumsNew) {
                if (!ulataVivariumsOld.contains(ulataVivariumsNewVivarium)) {
                    VivariumOperator oldVivariumOperatorOfUlataVivariumsNewVivarium = ulataVivariumsNewVivarium.getVivariumOperator();
                    ulataVivariumsNewVivarium.setVivariumOperator(vivariumOperator);
                    ulataVivariumsNewVivarium = em.merge(ulataVivariumsNewVivarium);
                    if (oldVivariumOperatorOfUlataVivariumsNewVivarium != null && !oldVivariumOperatorOfUlataVivariumsNewVivarium.equals(vivariumOperator)) {
                        oldVivariumOperatorOfUlataVivariumsNewVivarium.getUlataVivariums().remove(ulataVivariumsNewVivarium);
                        oldVivariumOperatorOfUlataVivariumsNewVivarium = em.merge(oldVivariumOperatorOfUlataVivariumsNewVivarium);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = vivariumOperator.getId();
                if (findVivariumOperator(id) == null) {
                    throw new NonexistentEntityException("The vivariumOperator with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            VivariumOperator vivariumOperator;
            try {
                vivariumOperator = em.getReference(VivariumOperator.class, id);
                vivariumOperator.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The vivariumOperator with id " + id + " no longer exists.", enfe);
            }
            List<Vivarium> ulataVivariums = vivariumOperator.getUlataVivariums();
            for (Vivarium ulataVivariumsVivarium : ulataVivariums) {
                ulataVivariumsVivarium.setVivariumOperator(null);
                ulataVivariumsVivarium = em.merge(ulataVivariumsVivarium);
            }
            em.remove(vivariumOperator);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<VivariumOperator> findVivariumOperatorEntities() {
        return findVivariumOperatorEntities(true, -1, -1);
    }

    public List<VivariumOperator> findVivariumOperatorEntities(int maxResults, int firstResult) {
        return findVivariumOperatorEntities(false, maxResults, firstResult);
    }

    private List<VivariumOperator> findVivariumOperatorEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(VivariumOperator.class));
            Query q = em.createQuery(cq);
            Query q1 = em.createNativeQuery("Select ID from vivariumoperator");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
                q1.setMaxResults(maxResults);
                q1.setFirstResult(firstResult);
            }
            List<VivariumOperator> vivariumOperatorsList = q.getResultList();
            for (int i = 0; i < vivariumOperatorsList.size(); i++) {
                vivariumOperatorsList.get(i).setId((Long) q1.getResultList().get(i));
            }
            return vivariumOperatorsList;
        } finally {
            em.close();
        }
    }

    public VivariumOperator findVivariumOperator(Long id) {
        EntityManager em = getEntityManager();
        try {
            VivariumOperator vivariumOperator = em.find(VivariumOperator.class, id);
            vivariumOperator.setId(id);
            return vivariumOperator;
        } finally {
            em.close();
        }
    }

    public int getVivariumOperatorCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<VivariumOperator> rt = cq.from(VivariumOperator.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
}
